# coding=utf-8
from BeautifulReport import BeautifulReport
import unittest, time, os
from common.readConfig import read
from common.email_send import ZmailSend
from common.caseLog import Log
from common.result_code import ResultCode

BASH_DIR = os.path.dirname(os.path.abspath(__file__))
log = Log()
def run_all():
    file_path = os.path.join(BASH_DIR, "case")
    log.info(file_path)
    discover = unittest.defaultTestLoader.discover(start_dir=file_path, pattern='message_test.py')
    report_path = os.path.join(BASH_DIR, "report/")
    time_str = time.strftime("%Y-%m-%d %H-%M-%S")
    file_r_path = "message_report.html"
    log.info(file_r_path)
    runner = BeautifulReport(discover)
    runner.report(
        filename=file_r_path,
        description='我的测试报告',
        log_path=report_path
    )

    # code = ResultCode(report_path + file_r_path)
    # email = ZmailSend()
    # html = '''<!DOCTYPE html>
    # <html lang="en">
    #     <head>
    #         <meta charset="UTF-8">
    #         <title>ffdfd</title>
    #         <img src="http://hua-cloud.cn/images/logo.png" alt="" style="text-align: center" width="">
    #         <style>
    #             body {
    #                 background-size: 1960px 1040px;
    #             }
    #             h1 {
    #                 text-align: center;
    #                 color: #cc0000;
    #             }
    #             .textdiv{
    #                 text-align: center;
    #             }
    #             .ptext{
    #                 text-align: center;
    #                 color: #1ab394;
    #                 font-size: 25px
    #             }
    #             .ptext1{
    #                 text-align: center ;
    #                 color: red;
    #                 font-size: 25px
    #             }
    #         </style>
    #     </head>
    #     <body background="https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2538026662,1778507730&fm=26&gp=0.jpg">
    #
    #         <h1 class="textClass" >测试结果如下：</h1>
    #             <table class="textdiv">
    #                 <p class="ptext">用例总数：%s</p>
    #                 <p class="ptext">用例通过：%s</p>
    #                 <p class="ptext">%s</p>
    #                 <p class="ptext1">用例失败：%s</p>
    #                 <p class="ptext1">%s</p>
    #                 <p class="ptext">测试时间：%s</p>
    #                 <p class="ptext">详情查看附件测试报告</p>
    #             </table>
    #     </body>
    # </html>''' % (code.testAll, code.testPass, code.success_rate(), code.testFail, code.error_rate(),
    #               code.beginTime)
    # msg = {
    #     "subject": '{time_str}权限中心接口自动化测试邮件!'.format(time_str=time_str),
    #     "content_html": html,
    #     "attachments": report_path + file_r_path
    # }
    # if code.success_code() != 1:
    #     '''测试有异常'''
    #     email_send_users = read('error_mail_send_cc', 'email').split(',')
    #     email_send_cc = read('error_mail_send_cc', 'ccemail').split(',')
    #     email.send(email_send_users, msg, cc=email_send_cc)
    # elif code.success_code() == 1:
    #     '''测试通过'''
    #     email_send_users = read('success_mail_send_user', 'email').split(',')
    #     email_send_cc = read('success_mail_send_user', 'ccemail').split(',')
    #     email.send(email_send_users, msg, cc=email_send_cc)


if __name__ == '__main__':
    run_all()